package com.coolgor.xcode;

import java.io.*;
import java.util.List;

public class StackController {

    public static void run(List<String[]> context,
                           String basePath,
                           String basePackage,
                           String moduleName) throws IOException {
        String outputPath = basePath + XcoderConfig.OutputPath + "/controller";;
        String tableName = null;
        String className = null;
        String keyColumn = null;
        String keyParam = null;
        for (int i = 0; i < context.size(); i++) {
            switch (context.get(i)[0].toLowerCase()) {
                case "table":
                    tableName = context.get(i)[1];
                    className = StringUtil.firstUpper(context.get(i)[2]);
                    break;
                case "key":
                    keyColumn = context.get(i)[1];
                    keyParam = context.get(i)[2];
                    break;
            }
        }

        // File writing ###############
        File file = new File(outputPath);
        if (!file.exists()) file.mkdirs();
        file = new File(outputPath + "/" + className + "Controller.java");
        if (!file.exists()) file.createNewFile();
        FileOutputStream fos = new FileOutputStream(file);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
        if (null!=basePackage) {
            if (basePackage.length()>1 && !basePackage.endsWith(".")) {
                basePackage = basePackage + ".";
            } else {
                basePackage = "";
            }
            if (null!=moduleName && !"".equals(moduleName) && !".".equals(moduleName)) {
                bw.write("package "+basePackage+"web."+moduleName+";\n");
                moduleName = moduleName+".";
            } else {
                bw.write("package "+basePackage+"web;\n");
                moduleName = "";
            }
            bw.newLine();
            bw.write("import "+basePackage+"common.HttpServletRequestUtil;\n");
            bw.newLine();
            bw.write("import "+basePackage+"entity."+moduleName+className+";\n");
            bw.write("import "+basePackage+"dto."+moduleName+className+"Execution;\n");
            bw.write("import "+basePackage+"enums."+moduleName+className+"OperationStateEnum;\n");
            bw.write("import "+basePackage+"service."+moduleName+className+"Service;\n");
        }
        bw.newLine();
        bw.write("import org.slf4j.Logger;\n");
        bw.write("import org.slf4j.LoggerFactory;\n");
        bw.write("//import org.springframework.stereotype.Controller;\n");
        bw.write("import org.springframework.beans.factory.annotation.Autowired;\n");
        bw.write("import org.springframework.web.bind.annotation.RequestBody;\n");
        bw.write("import org.springframework.web.bind.annotation.RequestMapping;\n");
        bw.write("import org.springframework.web.bind.annotation.RequestMethod;\n");
        bw.write("import org.springframework.web.bind.annotation.RequestParam;\n");
        bw.write("import org.springframework.web.bind.annotation.ResponseBody;\n");
        bw.write("import org.springframework.web.bind.annotation.RestController;\n");
        bw.newLine();
        bw.write("import javax.servlet.http.HttpServletRequest;\n");
        bw.newLine();
        bw.write("import java.util.List;\n");
        bw.write("import java.util.ArrayList;\n");
        bw.write("import java.util.Map;\n");
        bw.write("import java.util.HashMap;\n");
        bw.write("import java.util.Date;\n");
        bw.write("import java.text.SimpleDateFormat;\n");
        bw.write("import java.text.ParseException;\n");
        bw.newLine();
        bw.newLine();
        bw.write("@RestController  /* @Controller + @ResponseBody */\n");
        bw.write("@RequestMapping(value=\"/"+className+"\", produces=\"application/json;charset=utf-8\", method={RequestMethod.GET,RequestMethod.POST})\n");
        bw.write("public class "+className+"Controller {\n");
        bw.newLine();
        bw.write("\tprivate static Logger log = LoggerFactory.getLogger("+className+"Controller.class);\n");
        bw.newLine();
        bw.write("\t@Autowired\n");
        bw.write("\tprivate "+className+"Service "+StringUtil.firstLower(className)+"Service;\n");
        bw.newLine();
        bw.write("\t@RequestMapping(value = \"/getByID\", method={RequestMethod.GET,RequestMethod.POST})\n");
        bw.write("\tprivate Map<String, Object> getByID(HttpServletRequest request) {\n");
        bw.write("\t\tMap<String, Object> modelMap = new HashMap<String, Object>();\n");
        bw.write("\t\tLong "+keyParam+" = HttpServletRequestUtil.getLong(request, \""+keyParam.toLowerCase()+"\");\n");
        bw.write("\t\tif ("+keyParam+" > -1) {\n");
        bw.write("\t\t\ttry {\n");
        bw.write("\t\t\t\t"+className+"Execution exe = "+StringUtil.firstLower(className)+"Service.getByID("+keyParam+"); \n");
        bw.write("\t\t\t\tif ( exe.getState() == "+className+"OperationStateEnum.SUCCESS.getState() ) {\n");
        bw.write("\t\t\t\t\tmodelMap.put(\"success\", true);\n");
        bw.write("\t\t\t\t\tmodelMap.put(\""+className.toLowerCase()+"\", exe.get"+className+"());\n");
        bw.write("\t\t\t\t} else {\n");
        bw.write("\t\t\t\t\tmodelMap.put(\"success\", false);\n");
        bw.write("\t\t\t\t\tmodelMap.put(\"errMsg\", exe.getStateInfo());\n");
        bw.write("\t\t\t\t}\n");
        bw.write("\t\t\t} catch (Exception e) {\n");
        bw.write("\t\t\t\tmodelMap.put(\"success\", false);\n");
        bw.write("\t\t\t\tmodelMap.put(\"errMsg\", e.toString());\n");
        bw.write("\t\t\t}\n");
        bw.write("\t\t} else {\n");
        bw.write("\t\t\tmodelMap.put(\"success\", false);\n");
        bw.write("\t\t\tmodelMap.put(\"errMsg\", \"empty "+keyParam.toLowerCase()+"\");\n");
        bw.write("\t\t}\n");
        bw.write("\t\treturn modelMap;\n");
        bw.write("\t}\n");
        bw.newLine();
        bw.newLine();
        bw.write("\t@RequestMapping(value = \"/getList\", method={RequestMethod.GET,RequestMethod.POST})\n");
        bw.write("\tprivate Map<String, Object> getList(@RequestBody "+className+" "+StringUtil.firstLower(className)+"Condition, HttpServletRequest request) {\n");
        bw.write("\t\tMap<String, Object> modelMap = new HashMap<String, Object>();\n");
        bw.write("\t\tint page = HttpServletRequestUtil.getInt(request, \"page\");\n");
        bw.write("\t\tint pageSize = HttpServletRequestUtil.getInt(request, \"rows\");\n");
        bw.write("\t\tif ("+StringUtil.firstLower(className)+"Condition != null ) {\n");
        bw.write("\t\t\ttry {\n");
        bw.write("\t\t\t\t"+className+"Execution exe = "+StringUtil.firstLower(className)+"Service.getList("+StringUtil.firstLower(className)+"Condition, page, pageSize); \n");
        bw.write("\t\t\t\tif ( exe.getState() == "+className+"OperationStateEnum.SUCCESS.getState() ) {\n");
        bw.write("\t\t\t\t\tmodelMap.put(\"success\", true);\n");
        bw.write("\t\t\t\t\tmodelMap.put(\"total\",exe.getCount());\n");
        bw.write("\t\t\t\t\tmodelMap.put(\"rows\",exe.get"+className+"List());\n");
        bw.write("\t\t\t\t} else {\n");
        bw.write("\t\t\t\t\tmodelMap.put(\"success\", false);\n");
        bw.write("\t\t\t\t\tmodelMap.put(\"errMsg\", exe.getStateInfo());\n");
        bw.write("\t\t\t\t}\n");
        bw.write("\t\t\t} catch (Exception e) {\n");
        bw.write("\t\t\t\tmodelMap.put(\"success\", false);\n");
        bw.write("\t\t\t\tmodelMap.put(\"errMsg\", e.toString());\n");
        bw.write("\t\t\t}\n");
        bw.write("\t\t} else {\n");
        bw.write("\t\t\tmodelMap.put(\"success\", false);\n");
        bw.write("\t\t\tmodelMap.put(\"errMsg\", \"empty "+StringUtil.firstLower(className)+"Condition\");\n");
        bw.write("\t\t}\n");
        bw.write("\t\treturn modelMap;\n");
        bw.write("\t}\n");
        bw.newLine();
        bw.newLine();
        bw.write("\t@RequestMapping(value = \"/add\", method={RequestMethod.GET,RequestMethod.POST})\n");
        bw.write("\tprivate Map<String, Object> add(@RequestBody "+className+" "+StringUtil.firstLower(className)+", HttpServletRequest request) {\n");
        bw.write("\t\tMap<String, Object> modelMap = new HashMap<String, Object>();\n");
        bw.write("\t\tif ("+StringUtil.firstLower(className)+" != null ) {\n");
        bw.write("\t\t\ttry {\n");
        bw.write("\t\t\t\t"+className+"Execution exe = "+StringUtil.firstLower(className)+"Service.add("+StringUtil.firstLower(className)+"); \n");
        bw.write("\t\t\t\tif ( exe.getState() == "+className+"OperationStateEnum.SUCCESS.getState() ) {\n");
        bw.write("\t\t\t\t\tmodelMap.put(\"success\", true);\n");
        bw.write("\t\t\t\t\tmodelMap.put(\""+className.toLowerCase()+"\", exe.get"+className+"());\n");
        bw.write("\t\t\t\t} else {\n");
        bw.write("\t\t\t\t\tmodelMap.put(\"success\", false);\n");
        bw.write("\t\t\t\t\tmodelMap.put(\"errMsg\", exe.getStateInfo());\n");
        bw.write("\t\t\t\t}\n");
        bw.write("\t\t\t} catch (Exception e) {\n");
        bw.write("\t\t\t\tmodelMap.put(\"success\", false);\n");
        bw.write("\t\t\t\tmodelMap.put(\"errMsg\", e.toString());\n");
        bw.write("\t\t\t}\n");
        bw.write("\t\t} else {\n");
        bw.write("\t\t\tmodelMap.put(\"success\", false);\n");
        bw.write("\t\t\tmodelMap.put(\"errMsg\", \"empty "+StringUtil.firstLower(className)+"\");\n");
        bw.write("\t\t}\n");
        bw.write("\t\treturn modelMap;\n");
        bw.write("\t}\n");
        bw.newLine();
        bw.newLine();
        bw.write("\t@RequestMapping(value = \"/modify\", method={RequestMethod.GET,RequestMethod.POST})\n");
        bw.write("\tprivate Map<String, Object> modify(@RequestBody "+className+" "+StringUtil.firstLower(className)+", HttpServletRequest request) {\n");
        bw.write("\t\tMap<String, Object> modelMap = new HashMap<String, Object>();\n");
        bw.write("\t\tif ("+StringUtil.firstLower(className)+" != null ) {\n");
        bw.write("\t\t\ttry {\n");
        bw.write("\t\t\t\t"+className+"Execution exe = "+StringUtil.firstLower(className)+"Service.modify("+StringUtil.firstLower(className)+"); \n");
        bw.write("\t\t\t\tif ( exe.getState() == "+className+"OperationStateEnum.SUCCESS.getState() ) {\n");
        bw.write("\t\t\t\t\tmodelMap.put(\"success\", true);\n");
        bw.write("\t\t\t\t\tmodelMap.put(\""+className.toLowerCase()+"\", exe.get"+className+"());\n");
        bw.write("\t\t\t\t} else {\n");
        bw.write("\t\t\t\t\tmodelMap.put(\"success\", false);\n");
        bw.write("\t\t\t\t\tmodelMap.put(\"errMsg\", exe.getStateInfo());\n");
        bw.write("\t\t\t\t}\n");
        bw.write("\t\t\t} catch (Exception e) {\n");
        bw.write("\t\t\t\tmodelMap.put(\"success\", false);\n");
        bw.write("\t\t\t\tmodelMap.put(\"errMsg\", e.toString());\n");
        bw.write("\t\t\t}\n");
        bw.write("\t\t} else {\n");
        bw.write("\t\t\tmodelMap.put(\"success\", false);\n");
        bw.write("\t\t\tmodelMap.put(\"errMsg\", \"empty "+StringUtil.firstLower(className)+"\");\n");
        bw.write("\t\t}\n");
        bw.write("\t\treturn modelMap;\n");
        bw.write("\t}\n");
        bw.newLine();
        bw.newLine();
        bw.write("\t@RequestMapping(value = \"/remove\", method={RequestMethod.GET,RequestMethod.POST})\n");
        bw.write("\tprivate Map<String, Object> remove(@RequestParam(\"ids\") Long[] ids, HttpServletRequest request) {\n");
        bw.write("\t\tMap<String, Object> modelMap = new HashMap<String, Object>();\n");
        bw.write("\t\t/*\n");
        bw.write("\t\tString idString = HttpServletRequestUtil.getString(request, \"ids\");\n");
        bw.write("\t\tList<Long> list = new ArrayList<Long>();\n");
        bw.write("\t\tif (!\"\".equals(idString) && idString!=null) {\n");
        bw.write("\t\t\tString[] ids = idString.split(\",\");\n");
        bw.write("\t\t\tfor (int i=0; i<ids.length; i++)  {\n");
        bw.write("\t\t\t\tlist.add(Long.valueOf(ids[i]));\n");
        bw.write("\t\t\t}\n");
        bw.write("\t\t\t// 这里写执行的语句\n");
        bw.write("\t\t}\n");
        bw.write("\t\t*/\n");
        bw.write("\t\tif (ids != null && ids.length > 0) {\n");
        bw.write("\t\t\tList<Long> idList = new ArrayList<Long>();\n");
        bw.write("\t\t\tfor (int i=0; i<ids.length; i++) {\n");
        bw.write("\t\t\t\tidList.add(ids[i]);\n");
        bw.write("\t\t\t}\n");
        bw.write("\t\t\ttry {\n");
        bw.write("\t\t\t\t"+className+"Execution exe = "+StringUtil.firstLower(className)+"Service.removeBatchByID(idList); \n");
        bw.write("\t\t\t\tif ( exe.getState() == "+className+"OperationStateEnum.SUCCESS.getState() ) {\n");
        bw.write("\t\t\t\t\tmodelMap.put(\"success\", true);\n");
        bw.write("\t\t\t\t\tmodelMap.put(\"total\",exe.getCount());\n");
        bw.write("\t\t\t\t} else {\n");
        bw.write("\t\t\t\t\tmodelMap.put(\"success\", false);\n");
        bw.write("\t\t\t\t\tmodelMap.put(\"errMsg\", exe.getStateInfo());\n");
        bw.write("\t\t\t\t}\n");
        bw.write("\t\t\t} catch (Exception e) {\n");
        bw.write("\t\t\t\tmodelMap.put(\"success\", false);\n");
        bw.write("\t\t\t\tmodelMap.put(\"errMsg\", e.toString());\n");
        bw.write("\t\t\t}\n");
        bw.write("\t\t} else {\n");
        bw.write("\t\t\tmodelMap.put(\"success\", false);\n");
        bw.write("\t\t\tmodelMap.put(\"errMsg\", \"empty id list !\");\n");
        bw.write("\t\t}\n");
        bw.write("\t\treturn modelMap;\n");
        bw.write("\t}\n");
        bw.newLine();
        bw.newLine();
        bw.write("}\n");
        bw.close();
    }
}
